package com.example.calculationtest.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import com.example.calculationtest.MyViewModel
import com.example.calculationtest.R
import com.example.calculationtest.databinding.FragmentQuestionBinding
import kotlinx.android.synthetic.main.fragment_question.*
import java.lang.StringBuilder

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [QuestionFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class QuestionFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val stringBuilder = StringBuilder()
        val myViewModel by viewModels<MyViewModel>()
        val binding: FragmentQuestionBinding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_question, container, false)
        myViewModel.generator(20)
        binding.apply {
            data = myViewModel
            lifecycleOwner = activity
            buttonClean.setOnClickListener {
                binding.textViewAnswer.text = getString(R.string.textView_answer)
                stringBuilder.setLength(0)
            }
            buttonSubmit.setOnClickListener {
                if (stringBuilder.isEmpty()) {
                    Toast.makeText(requireContext(), R.string.answer_indicator_message, Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                val answerNumber = stringBuilder.toString().toInt()
                if (answerNumber ==  myViewModel.answer.value) {
                    myViewModel.answerCorrect()
                    stringBuilder.setLength(0)
                    binding.textViewAnswer.text = getString(R.string.answer_correct_message)
                } else {
                    val navController = Navigation.findNavController(it)
                    if (myViewModel.winFlag.value!!) {
                        navController.navigate(R.id.action_questionFragment_to_winFragment)
                        myViewModel.saveHighScore()
                        myViewModel.winFlag.value = false
                    } else {
                        navController.navigate(R.id.action_questionFragment_to_loseFragment)
                    }
                }
            }
        }

        val setButtons = setOf(
            binding.button0,
            binding.button1,
            binding.button2,
            binding.button3,
            binding.button4,
            binding.button5,
            binding.button6,
            binding.button7,
            binding.button8,
            binding.button9
        )

        setButtons.forEach {b ->
            b.setOnClickListener {
                val btn = it as Button
                if (stringBuilder.isEmpty()) {
                    stringBuilder.append(btn.text)
                }else if (stringBuilder.toString().toInt() < 65535) {
                    stringBuilder.append(btn.text)
                }
                binding.textViewAnswer.text = stringBuilder.toString()
            }
        }
        return binding.root
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment QuestionFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            QuestionFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}